package com.blog.common.domain.strategy;

import cn.hutool.extra.spring.SpringUtil;
import com.blog.common.domain.adapter.ConvertAdapter;
import com.blog.common.domain.annotation.Convert;

import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 字段处理类
 * </p>
 *
 * @author Valerie
 * @date 2023/5/28
 **/
public class ConvertStrategy {

    /**
     * 转换字段
     *
     * @param convert
     * @param filed
     * @return {@link String}
     */
    public static String covertByStrategy(Convert convert, String filed){
        Map<String, ConvertAdapter> adapterMap = SpringUtil.getBeansOfType(ConvertAdapter.class);
        Optional<ConvertAdapter> first = adapterMap.values().stream().filter(f -> f.getConvertAdapter(convert)).findFirst();
        return first.isPresent() ? first.get().convertField(convert, filed) : "";
    }
}
